//
//  QSMineHeaderView.m
//  DigitalEconomy_iOS
//
//  Created by Ding on 2020/5/15.
//  Copyright © 2020 DingC. All rights reserved.
//

#import "QSMineHeaderView.h"
#import "QSHomeInterface.h"
#import "QSOSSUploadHelper.h"
#import "QSUserInterface.h"
@interface QSMineHeaderView ()
/** 头部背景ImageView*/
@property (nonatomic, strong)UIImageView *headerBackgroundImageView;

/** 头部头像*/
@property (nonatomic, strong)UIImageView *userIconImageView;
/** 姓名Label*/
@property (nonatomic, strong)UILabel *nameLabel;
/** 企业Label*/
@property (nonatomic, strong)UILabel *enterpriseLabel;
/** 右侧箭头*/
@property (nonatomic, strong)UIImageView *rightImageView;

@property (nonatomic, strong) NSDictionary *userDic;

@end
@implementation QSMineHeaderView
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self configSubView];
        [self getUserInfo];//用户信息
         [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateUI) name:@"CHANGE_USERIMAGEFINISH" object:nil];
    }
    return self;
}

- (void)configSubView{
    [self addSubview:self.headerBackgroundImageView];
    
    [self.headerBackgroundImageView addSubview:self.userIconImageView];
    [self.headerBackgroundImageView addSubview:self.nameLabel];
    [self.headerBackgroundImageView addSubview:self.enterpriseLabel];
    [self.headerBackgroundImageView addSubview:self.rightImageView];
    
    [_headerBackgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.top.width.height.equalTo(self);
        //        make.height.offset(163);
    }];
    
    [_userIconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headerBackgroundImageView).offset(kRealValue(20));
        make.left.equalTo(self.headerBackgroundImageView).offset(kRealValue(25));
        make.width.height.offset(kRealValue(44));
    }];
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headerBackgroundImageView).offset(kRealValue(24));
        make.left.equalTo(self.userIconImageView.mas_right).offset(kRealValue(10));
    }];
    [_enterpriseLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLabel.mas_bottom).offset(kRealValue(5));
        make.left.equalTo(self.userIconImageView.mas_right).offset(kRealValue(10));
    }];
    [_rightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.headerBackgroundImageView.mas_trailing).offset(- kRealValue(24));
        make.centerY.equalTo(self.userIconImageView);
        make.width.height.offset(kRealValue(25));
    }];
}

#pragma mark - 懒加载
- (UIImageView *)headerBackgroundImageView{
    if (!_headerBackgroundImageView) {
        
        _headerBackgroundImageView = [[UIImageView alloc]init];
        
        _headerBackgroundImageView.userInteractionEnabled = YES;
        [_headerBackgroundImageView setImage:[UIImage imageNamed:@"ic_homeheder"]];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(selectBackImageView:)];
        [self.headerBackgroundImageView addGestureRecognizer:tap];
    }
    return _headerBackgroundImageView;
}

- (UIImageView *)userIconImageView{
    if (!_userIconImageView) {
        _userIconImageView = [[UIImageView alloc]init];
        _userIconImageView.layer.cornerRadius = 22;
        _userIconImageView.layer.masksToBounds = YES;
        _userIconImageView.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _userIconImageView;
}

- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [UILabel labelWithName:@"" font:[UIFont QSFontOfSize16] textColor:[UIColor QSColorWhiteFFFFFF] textAlignment:NSTextAlignmentLeft];
    }
    return _nameLabel;
}

- (UILabel *)enterpriseLabel{
    if (!_enterpriseLabel) {
        _enterpriseLabel = [UILabel labelWithName:@"" font:[UIFont QSFontOfSize12] textColor:[UIColor QSColorWhiteFFFFFF] textAlignment:NSTextAlignmentLeft];
    }
    return _enterpriseLabel;
}

- (UIImageView *)rightImageView{
    if (!_rightImageView) {
        _rightImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"ic_home_arrow"]];
//        _rightImageView.backgroundColor = [UIColor QSColorWhiteFFFFFF];
    }
    return _rightImageView;
}

#pragma mark - Action

- (void)selectBackImageView:(UITapGestureRecognizer *)sender{
    NSLog(@"点击了背景图");
    if (self.delegate && [self.delegate respondsToSelector:@selector(mineHeaderView:didSelectBackgroundPicture:)]) {
        [self.delegate mineHeaderView:self didSelectBackgroundPicture:1];
    }
}

#pragma mark - Api
- (void)getUserInfo{
    [QSHomeInterface getUserInfoAndBlock:^(ResponseMessage * _Nonnull rspStatusAndMessage, NSDictionary * _Nonnull result) {
        if (rspStatusAndMessage.code == kResponseSuccessCode) {
            self.userDic = result;
            QSUserModel *user = [QSUserManager sharedQSUserManager].user;
            user.name = self.userDic[@"name"];
            user.headPic = self.userDic[@"headPic"];
            user.phone = self.userDic[@"phone"];
            user.townshipName = self.userDic[@"townshipName"];
            user.roleName = self.userDic[@"roleName"];//角色名
            [[QSUserManager sharedQSUserManager] saveUser:user];
            [self updateUI];        }
        
    }];
}


//更新UI
- (void)updateUI{
    QSUserModel *user = [QSUserManager sharedQSUserManager].user;
    if (user.headPic && user.headPic.length > 1) {
        [self.userIconImageView sd_setImageWithURL:[NSURL URLWithString:user.headPic]];
    }else{
        UIImage *defaulUserIcon = [UIImage imageNamed:@"user_defult_icon"];
        [self.userIconImageView setImage:defaulUserIcon];
        //传到阿里云 并设置为默认头像
//        [self setDefaultUsericonWithAliOSSWithImage:defaulUserIcon];
    }
    [self.nameLabel setText:user.name];
    [self.enterpriseLabel setText:user.roleName];
}

//未设置图片则把默认头像上传到阿里云
- (void)setDefaultUsericonWithAliOSSWithImage:(UIImage *)image{
    [QSOSSUploadHelper asyncUploadFilesToOSS:@[image] complete:^(NSArray<NSString *> * _Nullable names, NSError * _Nullable error) {
        if (!error) {
            NSString *imageUrl = names.lastObject;
            DLog(@"%@", imageUrl);
//            上传到自己服务器
            [self changeUserImageWithImageUrl:imageUrl];
        }else{
        }
    }];
}
//发送到服务器更换
- (void)changeUserImageWithImageUrl:(NSString *)imageUrl{
    [QSUserInterface changeUserImageWithImageUrl:imageUrl AndBlock:^(ResponseMessage * _Nonnull rspStatusAndMessage, NSDictionary * _Nonnull result) {
        if (rspStatusAndMessage.code == kResponseSuccessCode) {
            kCurrentUser.headPic = imageUrl;
            [[QSUserManager sharedQSUserManager] saveUser:kCurrentUser];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"CHANGE_USERIMAGEFINISH" object:nil];
        }else{
            
        }
    }];
}


- (void)dealloc{
    //移除观察者
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
